package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DetailsActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView Iv_Img;
    TextView Tv_Name,Tv_Price,Tv_Detailss;
    Button Btn_Yes,Btn_No;
    private int uid;
    private static final String TAG = "DetailsActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        Iv_Img = findViewById(R.id.Iv_Img);

        Tv_Name = findViewById(R.id.Tv_Name);
        Tv_Price = findViewById(R.id.Tv_Price);
        Tv_Detailss = findViewById(R.id.Tv_Detailss);

        Btn_Yes = findViewById(R.id.Btn_Yes);
        Btn_No = findViewById(R.id.Btn_No);

        Btn_Yes.setOnClickListener(this);
        Btn_No.setOnClickListener(this);

        Intent intent = getIntent();
        uid = intent.getIntExtra("id",0);


        show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Btn_Yes:
                Intent intent = new Intent(DetailsActivity.this,OrderActivity.class);
                intent.putExtra("uid",uid);
                startActivity(intent);
                break;

            case R.id.Btn_No:
                Intent intent1 = new Intent(DetailsActivity.this,FoodListActivity.class);
                startActivity(intent1);
                break;
        }
    }

    private void show() {
        new Thread(new Runnable() {
            private static final String TAG = "MainActivity";
            @Override
            public void run() {
                //1 client对象
                OkHttpClient client = new OkHttpClient();
                //2 Request对象
                FormBody body = new FormBody.Builder()

                        .build();
                Request request = new Request.Builder()
                        .url("http://10.2.64.68:8080/juyuan/foods/getFoods?id="+uid)
                        .post(body)
                        .build();
                //3 call对象
                final Call call = client.newCall(request);
                //4 执行call对象得到数据
                try {
                    Response response = call.execute();
                    final String data = response.body().string();
                    Gson gson = new Gson();
                    getFoodGson gu = gson.fromJson(data, getFoodGson.class);


                    Tv_Name.setText(gu.getName());
                    Tv_Price.setText(gu.getPrice()+"元");
                    Tv_Detailss.setText(gu.getDetails());

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).start();

    }
    }